import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import geopandas as gp
import shapely
import bokeh, bokeh.plotting, bokeh.models
from bokeh.io import output_notebook, show
output_notebook()
%matplotlib inline
Trees = pd.read_csv("2015_Street_Tree_Census_-_Tree_Data.csv",low_memory=False)
Trees.info()
hoods = gp.GeoDataFrame.from_file("C:/Users/julie/Documents/Kaggle/Trees/Neighborhood Tabulation Areas/")
hoods.info()
hoods["nta_num"]= pd.Categorical(hoods.ntaname).codes
##convert the geodataframe into a bokeh data source
gjds = bokeh.models.GeoJSONDataSource(geojson=hoods.to_json())
##input the tools you wish to use in your plot
TOOLS = "pan,wheel_zoom,reset,hover,save"
##create the plot
p = bokeh.plotting.figure(title="NYC Neighborhoods", tools=TOOLS,
x_axis_location=None, y_axis_location=None,sizing_mode='fixed')
##choose your color scheme to map
color_mapper = bokeh.models.LinearColorMapper(palette=bokeh.palettes.Category20[20])
##map the color scheme to each of the neighborhoods
p.patches('xs', 'ys',
fill_color={'field': 'nta_num', 'transform': color_mapper},
fill_alpha=1., line_color="black", line_width=0.5,
source=gjds)
##Remove the grid
p.grid.grid_line_color = None
##Create the interactive hovertool
hover = p.select_one(bokeh.models.HoverTool)
hover.point_policy = "follow_mouse"
hover.tooltips = u"""
<div>
<div class="bokeh_hover_tooltip">Name : @ntaname</div>
<div class="bokeh_hover_tooltip">Borough : @boro_name</div>
<div class="bokeh_hover_tooltip">Zone ID : @county_fip</div>
<div class="bokeh_hover_tooltip">(Lon, Lat) : ($x ËšE, $y ËšN)</div>
</div>
"""
show(p)
from geopandas import GeoDataFrame
from shapely.geometry import Point
geometry = [Point(xy) for xy in zip(Trees.longitude, Trees.latitude)]
Trees = Trees.drop(['longitude', 'latitude'], axis=1)
crs = {'init': 'epsg:4326'}
Trees = GeoDataFrame(Trees, crs=crs, geometry=geometry)
Trees = gp.sjoin(Trees, hoods, how="inner", op='intersects')
hoods["trees"] = hoods["ntaname"].map(Trees.groupby("ntaname").size().to_dict())
hoods["blocks"] = hoods["shape_area"] / 100000
hoods["tree_block"] = hoods["trees"] / hoods["blocks"]
gjds = bokeh.models.GeoJSONDataSource(geojson=hoods.to_json())
TOOLS = "pan,wheel_zoom,reset,hover,save"
p = bokeh.plotting.figure(title="NYC Neighborhoods", tools=TOOLS,
x_axis_location=None, y_axis_location=None,sizing_mode='fixed')
color_mapper = bokeh.models.LinearColorMapper(bokeh.palettes.Greens[(5)][::-1])
p.patches('xs', 'ys',
fill_color={'field': 'tree_block', 'transform': color_mapper},
fill_alpha=1., line_color="black", line_width=0.5,
source=gjds)
p.grid.grid_line_color = None
p.background_fill_color = "beige"
p.background_fill_alpha = .05
hover = p.select_one(bokeh.models.HoverTool)
hover.point_policy = "follow_mouse"
hover.tooltips = u"""
<div>
<div class="bokeh_hover_tooltip">Name : @ntaname</div>
<div class="bokeh_hover_tooltip">Borough : @boro_name</div>
<div class="bokeh_hover_tooltip">Zone ID : @county_fip</div>
<div class="bokeh_hover_tooltip">Trees per Block : @tree_block</div>
<div class="bokeh_hover_tooltip">(Lon, Lat) : ($x ËšE, $y ËšN)</div>
</div>
"""
show(p)
Trees.groupby(["ntaname","status"]).size()
hoods["green_trees"] = hoods["ntaname"].map(Trees[Trees["status"] == "Alive"].groupby("ntaname").size().to_dict())
hoods["green_block"] = hoods["green_trees"] / hoods["blocks"]
gjds = bokeh.models.GeoJSONDataSource(geojson=hoods.to_json())
TOOLS = "pan,wheel_zoom,reset,hover,save"
p = bokeh.plotting.figure(title="NYC Neighborhoods", tools=TOOLS,
x_axis_location=None, y_axis_location=None,sizing_mode='fixed')
color_mapper = bokeh.models.LinearColorMapper(bokeh.palettes.RdYlGn[(9)][::-1])
p.patches('xs', 'ys',
fill_color={'field': 'green_block', 'transform': color_mapper},
fill_alpha=1., line_color="black", line_width=0.5,
source=gjds)
p.grid.grid_line_color = None
p.background_fill_color = "beige"
p.background_fill_alpha = .05
hover = p.select_one(bokeh.models.HoverTool)
hover.point_policy = "follow_mouse"
hover.tooltips = u"""
<div>
<div class="bokeh_hover_tooltip">Name : @ntaname</div>
<div class="bokeh_hover_tooltip">Borough : @boro_name</div>
<div class="bokeh_hover_tooltip">Zone ID : @county_fip</div>
<div class="bokeh_hover_tooltip">Trees per Block : @green_block</div>
<div class="bokeh_hover_tooltip">(Lon, Lat) : ($x ËšE, $y ËšN)</div>
</div>
"""
show(p)